'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	// 查询避坑合同
	if (event.type === 'refuge') {
		const res = await db.collection('refuge').get();
		console.log(res)
		try {
			return {
				code: 200,
				message: '查询成功',
				data: res.data
			}
		} catch (e) {
			return {
				code: 500,
				data: [],
				message: '当前网络错误！'
			}
			//TODO handle the exception
		}
	}
	// 发布
	if (event.type === 'publish') {
		const res = await db.collection('publish').add(event)
		try {
			return {
				code: 200,
				message: '发布成功',
				data: res.data
			}
		} catch (e) {
			return {
				code: 500,
				data: [],
				message: '当前网络错误！'
			}
			//TODO handle the exception
		}

	}
	// 首页查询
	if(event.type==='home'){
		const { page,size} = event
		// const user = await db.collection('publish').field('images,num1,sexname,roomname,hallsname,roomTypename,address,price').getTemp()
		const res = await db.collection('publish').field({//过滤，将这些字段进行返回
			'images':true,
			'num1':true,
			'sexname':true,
			'roomname':true,
			'hallsname':true,
			'roomTypename':true,
			'address':true,
			'price':true
		}).get()
		// .skip((page-1)*size).limit(size).get()
		try {
			return {
				code: 200,
				message: '查询成功',
				data: res.data
			}
		} catch (e) {
			return {
				code: 500,
				data: [],
				message: '当前网络错误！'
			}
			//TODO handle the exception
		}
	}
	//返回数据给客户端
	return event
};